package com.avalon.shop.service;

import com.avalon.core.exception.AvalonException;
import com.avalon.core.model.PageParam;
import com.avalon.core.util.JacksonUtil;
import com.avalon.core.util.RestTemplateUtils;
import com.avalon.shop.ShopContext;
import com.avalon.shop.config.ShopConfig;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class FavoriteService extends BaseService {
    public FavoriteService(ShopConfig shopConfig, ShopContext shopContext) {
        super(shopConfig, shopContext);
    }

    @Override
    protected String getServiceName() {
        return "pet.favorite";
    }


    public List<Map> getAllFavorite(Integer userId) {
        HashMap<String, Object> param = new HashMap<>();
        PageParam pageParam = new PageParam();
        pageParam.setPageNum(1);
        pageParam.setPageSize(1000);
        param.put("page", pageParam);
        param.put("fields", "id,userId,commodityId.id,commodityId.name,commodityId.firstUrl," +
                "commodityId.price,commodityId.petCategoryTags.id," +
                "commodityId.petCategoryTags.petBigTypeId.id," +
                "commodityId.petCategoryTags.petBigTypeId.name");
        param.put("rpnCondition", "(=,userId," + userId + ")");
        param.put("serviceName", getServiceName());
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/get/page/condition/object",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            Map maps = JacksonUtil.convert2Object(responseEntity.getBody(),
                    new TypeReference<Map>() {
                    });
            return (List<Map>) maps.get("data");
        } else {
            log.error("getAllFavorite 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }

    public List<Map> getOneFavorite(Integer userId, Integer commodityId) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("fields", "id,userId,commodityId.id,commodityId.name,commodityId.firstUrl," +
                "commodityId.price");
        param.put("rpnCondition", "(&,(=,userId," + userId + "),(=,commodityId," + commodityId + ")");
        param.put("serviceName", getServiceName());
        Map<String, String> headers = new HashMap<>();
        headers.put("content-type", "application/json");
        headers.put("token", shopContext.getBackToken());
        headers.put("Accept", MediaType.toString(Arrays.asList(MediaType.APPLICATION_JSON)));
        ResponseEntity<String> responseEntity = RestTemplateUtils.post(
                shopConfig.getServer() + "/model/get/all",
                headers, param, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            List<Map> maps = JacksonUtil.convert2Object(responseEntity.getBody(),
                    new TypeReference<List<Map>>() {
                    });
            return maps;
        } else {
            log.error("getAllFavorite 错误:" + responseEntity.getBody());
            throw new AvalonException(responseEntity.getBody());
        }
    }
}
